/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    //1，将所用的变量都抽取出来，放在页面加载后
    var history_datagrid = $("#history_datagrid");
    var history_dialog = $("#history_dialog");
    var history_form = $("#history_form");
    var import_dialog = $("#import_dialog");


    //产品
    var choose1_product_dialog = $("#choose1_product_dialog");
    var product1_datagrid = $("#product1_datagrid");

    choose1_product_dialog.dialog({
        width: 800,
        height: 350,
        closed: true,
        modal: true
    });



    //产品
    product1_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        toolbar: '#product_toolbar',
        url:"/temporary/selectByPrimaryKey.do",
        pagination: true,
        rownumbers:true,
        ctrlSelect: true,
        columns: [[
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {field: 'organizationId', title: '保险机构', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : '';
                }},
            {field: 'organizationDate', title: '保障年限', width: 100},
            {field: 'imit', title: '保额', width: 100},
            {field: 'annuity', title: '基本年费', width: 100},
            {field: 'status', title: '销售状态', width: 100,
                formatter: function (value, row, index) {
                    return value ? "<font color='blue'>在售</font>" : "<font color='red'>停售</font>"
                }
            },
            {field: 'plan', title: '不计免赔', width: 100,
                formatter: function (value, row, index) {
                    return value ? "<font color='blue'>是</font>" : "<font color='red'>否</font>"
                }
            }
        ]]
    });
    history_datagrid.datagrid({
        url: '/temporary/queryRecord.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: '#tb',
        columns: [[
            {field: 'sn', title: '保单编号', width: 100},
            {
                field: 'customer', title: '投保人', width: 100, align: 'center', formatter: function (value, row, index) {
                return value ? value.name : "";
            }
            },
            // {
            //     field: 'car', title: '车牌号', width: 100, align: 'center', formatter: function (value, row, index) {
            //     return value ? value.plateNumber : "";
            // }
            // },
            {field: 'beginDate', title: '开始日期', width: 100, align: 'center'},

            {field: 'checkDate', title: '核保日期', width: 100, align: 'center'},
            {field: 'endDate', title: '保险截止日', width: 100, align: 'center'},
            {field: 'duration', title: '保险时长', width: 100, align: 'center'},
            {
                field: 'inputUser', title: '业务员', width: 100, align: 'center', formatter: function (value, row, index) {
                return value ? value.username : "";
            }
            },
            {
                field: 'auditor', title: '审核人', width: 100, align: 'center', formatter: function (value, row, index) {
                return value ? value.username : "";
            }
            },
            {field: 'totalAmout', title: '投保总金额', width: 100, align: 'center'},
            {field: 'state', title: '状态', width: 100, align: 'center', formatter: function (value, row, index) {
                var state="";
                if (value==0){
                    state ="暂存待提交"
                } else if (value==1){
                    state ="未审核"
                } else if (value==2){
                    state ="退回修改"
                } else if (value==3){
                    state="<font color='red'>拒保</font>";
                } else if (value==4){
                    state ="已审核待缴费"
                } else if (value==5){
                    state ="<font color='green'>已缴费</font>"
                }
                return state;
            }},
            // {
            //     field: 'detail', title: '查看明细', width: 100, align: 'center', formatter: function (value, row, index) {
            //     return "<a href='#' id='detail' onclick='Showgoods();'>保险产品明细</a>";
            // }
            // }

        ]],
    })
    history_dialog.dialog({
        width: 1000,
        height: 600,
        buttons:'#form_btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            history_form.form('clear');
        }
    })
    import_dialog.dialog({
        width: 350,
        height: 100,
        closed: true
    })

    //2，将所有方法同一交给一个对象管理
    var methodObj = {


        reload: function () {
            history_datagrid.datagrid('reload');
        },

        cancel: function () {
            history_dialog.dialog('close');
        },
        query: function () {
            var namekeyword = $("[name=namekeyword]").val();
            var snkeyword = $("[name=snkeyword]").val();
            history_datagrid.datagrid('load', {
                namekeyword: namekeyword,
                snkeyword:snkeyword,
            })
        },
        Showgoods:function () {
            console.log(55555555)
            var row = history_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中数据', 'warning');
                return;
            }
            $.get('/temporary/selectByPrimaryKey.do', {id: row.product.id}, function (data) {
                product1_datagrid.datagrid("loadData", data);
                choose1_product_dialog.dialog("setTitle", "保险产品列表");
                choose1_product_dialog.dialog('open');

            })


        },
        exportXls: function () {
            var keyword = $("[name=keyword]").val();
            var deptId = $("[name=deptId]").val();
            window.location.href = "/employee/exportXls.do?keyword=" + keyword + "&deptId=" + deptId;
        },
        importXls: function () {
            import_dialog.dialog('open');
        }
    }
    //3，给按钮统一绑定点击事件
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
function fileSubmit() {
    var fileSize = $("#file").val();
    if(fileSize!=null && fileSize != ''){
        console.log("bu");
        $("#import_form").submit();
    }else {
        $.messager.alert('温馨提示', '请选中要上传的文件！', 'warning');
    }
}
